/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ulss.monitor.mainserver;

import static cn.ac.iie.ulss.monitor.mainserver.LoadrateHandler.logger;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

/**
 *
 * @author fxszd
 */
public class LoadMqStatusHandler extends AbstractHandler {

    private static LoadMqStatusHandler mqStatusHandler = null;
    static Logger logger = Logger.getLogger(LoadMqStatusHandler.class.getName());

    private LoadMqStatusHandler() {
    }

    public static LoadMqStatusHandler getMqStatusHandler() {
        if (mqStatusHandler == null) {
            mqStatusHandler = new LoadMqStatusHandler();
        }
        return mqStatusHandler;
    }

    /**
     *
     * @param string
     * @param baseRequest
     * @param httpServletRequest
     * @param httpServletResponse
     * @throws IOException
     * @throws ServletException
     */
    //http://192.168.10.185:8021/loadrate/json/
    @Override
    public void handle(String string, Request baseRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
        baseRequest.setHandled(true);
        String remoteHost = baseRequest.getRemoteAddr();
        int remotePort = baseRequest.getRemotePort();
        String reqID = String.valueOf(System.nanoTime());
        logger.info("receive request from " + remoteHost + ":" + remotePort + " and assigned id " + reqID);
        String mqStatus = null;
        String finalrate = null;
        String group = null;
        String tps = null;
        String diff = null;
        List<Object> list = new ArrayList<Object>();
        try {
            FileReader reader = new FileReader("mqStatus");
            BufferedReader bufedreader = new BufferedReader(reader);
            while (true) {
                mqStatus = bufedreader.readLine();
                if (mqStatus == null) {
                    break;
                }
                if (mqStatus.split("[|]").length < 7) {
                    group = mqStatus.split("[|]")[0];
                    tps = mqStatus.split("[|]")[2];
                    diff = mqStatus.split("[|]")[3];
                } else {
                    group = mqStatus.split("[|]")[0];
                    tps = mqStatus.split("[|]")[5];
                    diff = mqStatus.split("[|]")[6];
                }

//                if (group.equalsIgnoreCase("#Group")) {
//                    continue;
//                }
                Map<String, String> map = new HashMap<String, String>();
                map.put("GroupName", group);
                map.put("TPS", tps);
                map.put("DiffTotal", diff);
                list.add(map);
            }
        } catch (Exception ex) {
            logger.error(ex, ex);
        }
        JSONArray jsonObj = JSONArray.fromObject(list);
        String info = "{result:'success', list:" + jsonObj.toString() + "}";
        //String info =  jsonObj.toString();
        JSONObject obj = JSONObject.fromObject(info);
        logger.info(obj);

        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        httpServletResponse.getWriter().println(obj);
    }
}
